/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mp.appApi.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @Date: 130916
 * @author vannh
 */
public class XmlUtils {
    public Document parse(String path) throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File(path);
        Document document = reader.read(file);
        return document;
    }
    
    /*
     * Get a list of element from any element
     */
    public List<Element> getElementsFromRoot(Element rootElement){
        List<Element> els = new ArrayList<Element>();
        
        for (Iterator i = rootElement.elementIterator(); i.hasNext(); ) {
            Element e = (Element) i.next();
            els.add(e);
        }
        
        return els;
    }
    
    /*
     * Get a list of element from any element by element name
     */
    public List<Element> getElementsByName(Element rootElement, String elName){
        List<Element> els = new ArrayList<Element>();
        
        for(Iterator i = rootElement.elementIterator(elName); i.hasNext();) {
            Element e = (Element) i.next();
            els.add(e);
        }
        
        return els;
    }
    
    /*
     * Get value element by name
     */
    public String getValueElementByName(Element rootElement, String elName){
        String result = null;
        
        for (Iterator i = rootElement.elementIterator(elName); i.hasNext();) {
            Element e = (Element) i.next();
            result = e.getTextTrim();
            break;
        }
        
        return result;
    }
}